#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/6/19 0019 20:19
# @Author  : wzy
# @Site    : 
# @File    : data_codition.py
# @Software: PyCharm

from Util.operate_excel import operateExcel
# 命令行中  安装:pip install jsonpath-rw
from jsonpath_rw import parse
from Util.operate_excel import operateExcel
import json

def split_excel_data(data):
    '''
    分割数据
    :param data: 参数
    :return: 返回结果
    '''
    #imooc_005>data:banner:id
    case_id = data.split(">")[0]  #imooc_005 依赖ID
    rule_data = data.split(">")[1] # data:banner:id
    return case_id,rule_data

def depend_data(data):
    '''
    分割数据或，获取依赖ID，根据依赖ID 查找到对应依赖ID的 返回依赖ID的返回数据
    注意  依赖ID要小于该用例（要是被依赖的用例ID 在该用例后边，那么依赖ID返回结果为空）
    :param data:
    :return:
    '''
    case_id = split_excel_data(data)[0] # 依赖ID
    # 获取行号
    row_number = operateExcel.get_rows_number(case_id)
    data = operateExcel.get_cell_value(row_number,14)
    return data

def get_depend_value(res_data, key):
    '''
    :param res_data:
    :param key:
    :return:
    '''
    # if isinstance(res_data,str):
    #     res_data = dict(res_data)
    #     print(type(res_data))
    json_exe = parse(key)
    # 以某种格式拆分数据之后按照特定格式获取数据  --->data.banner.[0].id
    # 实际场景中根据接口返回的内容合理调整结构
    # DBUG 模式下观察madle   -
    # 返回内容只有一个 ，并且从外到内的顺序是 id->【0】->banner->data->this
    # 其中json_exe是Child
    madle = json_exe.find(eval(res_data))
    data = [i.value for i in madle][0]
    return  data # lambda

def get_depend_data(data):
    '''
    :param data:
    :return:获取依赖数据
    '''
    res_data = depend_data(data)
    rule_data = split_excel_data(data)[1]
    return get_depend_value(res_data,rule_data)

if __name__ == '__main__':
    # data = {
    #     "a": "a1",
    #     "b": "b1",
    #     "c": [
    #         {
    #             "d": "d1"
    #         },
    #         {
    #             "d": "d2"
    #         }
    #     ]
    # }
    # key = 'c.[1].d'
    key = 'data.banner.[0].id'
    data = {"status": 0,
            "data":# data
                    {
                        "banner":# banner
                                    # ↓数列   【0】就是列表ID = 2262的  【1】就是ID= 1648的
                            [{"id": 2262, "type": 6, "type_id": 330, "name": "\u524d\u7aef\u4e0b\u4e00\u4ee3\u5f00\u53d1\u8bed\u8a00TypeScript  \u4ece\u57fa\u7840\u5230axios\u5b9e\u6218", "pic": "http://szimg.mukewang.com/5cf721df09fc2be500000000.jpg", "links": ""},
                             {"id": 1648, "type": 6, "type_id": 169, "name": "Python3\u5165\u95e8\u673a\u5668\u5b66\u4e60 \u7ecf\u5178\u7b97\u6cd5\u4e0e\u5e94\u7528", "pic": "http://szimg.mukewang.com/5d0ed2d9085bd6ed09000300.jpg", "links": ""}, {"id": 1875, "type": 6, "type_id": 316, "name": "\u4ece\u57fa\u7840\u5230\u5b9e\u6218 \u624b\u628a\u624b\u5e26\u4f60\u638c\u63e1\u65b0\u7248Webpack4.0", "pic": "http://szimg.mukewang.com/5d0ed2ca086a9e6f09000300.jpg", "links": ""}, {"id": 1999, "type": 6, "type_id": 342, "name": "\u7eaf\u6b63\u5546\u4e1a\u7ea7\u5e94\u7528 Node.js Koa2\u5f00\u53d1\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u670d\u52a1\u7aef", "pic": "http://szimg.mukewang.com/5ceb5d370955f30f09000300.jpg", "links": ""}, {"id": 2158, "type": 99, "type_id": 0, "name": "Spring Cloud\u5fae\u670d\u52a1\u5f00\u53d1\u5b9e\u8df5", "pic": "http://img2.mukewang.com/5d088c4009bbebc009000300.jpg", "links": "https://www.imooc.com/read/37"}, {"id": 1709, "type": 6, "type_id": 354, "name": "Node.js\u5f00\u53d1\u4eff\u77e5\u4e4e\u670d\u52a1\u7aef \u6df1\u5165\u7406\u89e3RESTful API", "pic": "http://szimg.mukewang.com/5d0ed27508f7d96909000300.jpg", "links": ""}], "pic": [{"pic": "http://www.imooc.com/static/img/andriod/pic/actual_day@3x.png", "pic_night": "http://www.imooc.com/static/img/andriod/pic/actual_night@3x.png", "type": 2}, {"pic": "http://www.imooc.com/static/img/andriod/pic/path_day@3x.png", "pic_night": "http://www.imooc.com/static/img/andriod/pic/path_night@3x.png", "type": 6}, {"pic": "http://www.imooc.com/static/img/andriod/pic/question_day@3x.png", "pic_night": "http://www.imooc.com/static/img/andriod/pic/question_night@3x.png", "type": 3}, {"pic": "http://www.imooc.com/static/img/andriod/pic/note_day@3x.png", "pic_night": "http://www.imooc.com/static/img/andriod/pic/note_night@3x.png", "type": 4}, {"pic": "http://www.imooc.com/static/img/andriod/pic/discover_day@3x.png", "pic_night": "http://www.imooc.com/static/img/andriod/pic/discover_night@3x.png", "type": 5}]}, "errorCode": 1001, "errorDesc": "\u6210\u529f", "timestamp": 1561269343507
            }
    data1 = {"status": 0,
             "data":
                 {"banner":
                      [{"id": 2262, "type": 6, "type_id": 330, "name": "\u524d\u7aef\u4e0b\u4e00\u4ee3\u5f00\u53d1\u8bed\u8a00TypeScript  \u4ece\u57fa\u7840\u5230axios\u5b9e\u6218", "pic": "http://szimg.mukewang.com/5cf721df09fc2be500000000.jpg", "links": ""}, {"id": 1648, "type": 6, "type_id": 169, "name": "Python3\u5165\u95e8\u673a\u5668\u5b66\u4e60 \u7ecf\u5178\u7b97\u6cd5\u4e0e\u5e94\u7528", "pic": "http://szimg.mukewang.com/5d0ed2d9085bd6ed09000300.jpg", "links": ""}, {"id": 1875, "type": 6, "type_id": 316, "name": "\u4ece\u57fa\u7840\u5230\u5b9e\u6218 \u624b\u628a\u624b\u5e26\u4f60\u638c\u63e1\u65b0\u7248Webpack4.0", "pic": "http://szimg.mukewang.com/5d0ed2ca086a9e6f09000300.jpg", "links": ""}, {"id": 1999, "type": 6, "type_id": 342, "name": "\u7eaf\u6b63\u5546\u4e1a\u7ea7\u5e94\u7528 Node.js Koa2\u5f00\u53d1\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u670d\u52a1\u7aef", "pic": "http://szimg.mukewang.com/5ceb5d370955f30f09000300.jpg", "links": ""}, {"id": 2158, "type": 99, "type_id": 0, "name": "Spring Cloud\u5fae\u670d\u52a1\u5f00\u53d1\u5b9e\u8df5", "pic": "http://img2.mukewang.com/5d088c4009bbebc009000300.jpg", "links": "https://www.imooc.com/read/37"}, {"id": 1709, "type": 6, "type_id": 354, "name": "Node.js\u5f00\u53d1\u4eff\u77e5\u4e4e\u670d\u52a1\u7aef \u6df1\u5165\u7406\u89e3RESTful API", "pic": "http://szimg.mukewang.com/5d0ed27508f7d96909000300.jpg", "links": ""}], "pic": [{"pic": "http://www.imooc.com/static/img/andriod/pic/actual_day@3x.png", "pic_night": "http://www.imooc.com/static/img/andriod/pic/actual_night@3x.png", "type": 2}, {"pic": "http://www.imooc.com/static/img/andriod/pic/path_day@3x.png", "pic_night": "http://www.imooc.com/static/img/andriod/pic/path_night@3x.png", "type": 6}, {"pic": "http://www.imooc.com/static/img/andriod/pic/question_day@3x.png", "pic_night": "http://www.imooc.com/static/img/andriod/pic/question_night@3x.png", "type": 3}, {"pic": "http://www.imooc.com/static/img/andriod/pic/note_day@3x.png", "pic_night": "http://www.imooc.com/static/img/andriod/pic/note_night@3x.png", "type": 4}, {"pic": "http://www.imooc.com/static/img/andriod/pic/discover_day@3x.png", "pic_night": "http://www.imooc.com/static/img/andriod/pic/discover_night@3x.png", "type": 5}]}, "errorCode": 1001, "errorDesc": "\u6210\u529f", "timestamp": 1561269343507}
    data2 = operateExcel.get_cell_value(2,14)
    print(get_depend_value(data2,key))
    pass